package com.example.demo1;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class FileUtils {
    public static boolean upload(MultipartFile file, String path, String oldFileName, String fileName){

        if(!oldFileName.isEmpty()){
            File oldFile = new File(path + "/" + oldFileName);
            //判断文件父目录是否存在
            if(oldFile.exists()){
                boolean deleteResult = oldFile.delete();
            }
        }

        File dest = new File(path + "/" + fileName);

        //判断文件父目录是否存在
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }

        try {
            //保存文件
            file.transferTo(dest);
            return true;
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }

    }
}
